package 行为型模式.中介者模式;

import java.util.ArrayList;
import java.util.List;

/**
 * 主要角色
 * Mediator (中介者接口): 定义各个同事对象之间通信的接口
 * ConcreteMediator (具体中介者): 实现中介者接口，协调各个同事对象
 * Colleague (同事类): 每个同事对象都知道它的中介者对象，与其他同事对象通信时都通过中介者进行
 */

class User
{
	private String name;
	private ChartRoom chartRoom;
	
	public User(String name,ChartRoom chartRoom)
	{
		this.name = name;
		this.chartRoom = chartRoom;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public ChartRoom getCharacter() {
		return chartRoom;
	}

	public void setCharacter(ChartRoom character) {
		this.chartRoom = character;
	}
	
	public void send(String mes)
	{
		System.out.println(name+"发送："+mes);
		chartRoom.send(mes,this);
	}
}

class ChartRoom
{
	private List<User> users = new ArrayList();
	
	public void send(String mes,User user)
	{
		//给每个用户发送消息
		users.stream().forEach(u->{
			if(!u.equals(user))
			{
				System.out.println(u.getName()+"接受："+mes);
			}
		});
	}
	
	public void addUser(User user)
	{
		users.add(user);
	}
}

public class Mediator 
{
	public static void main(String[] args) 
	{
		//聊天室
		ChartRoom chartRoom = new ChartRoom();
		//用户加入聊天室
		User a = new User("A",chartRoom);
		User b = new User("B",chartRoom);
		
		//聊天室允许用户加入
		chartRoom.addUser(a);
		chartRoom.addUser(b);
		
		a.send("大家好");
		b.send("你好");
	}
}
